package com.dacool.fragment;

import android.graphics.Color;
import android.net.http.SslError;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.JsResult;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.dacool.activity.MainActivity;
import com.dacool.util.ViewUtils;

public class WebViewFragment extends BaseFragment {

	private String url;
	private WebView mWebView;
	private String mTitle;
	private String data;
	private boolean pressBackToFinish = false;

	@Override
	protected int myLayoutID() {
		// TODO Auto-generated method stub
		return 0;
	}

	public void setData(String data) {
		this.data = data;
	}

	@Override
	protected View myView() {
		// TODO Auto-generated method stub
		mWebView = new WebView(getActivity());
		mWebView.setBackgroundColor(Color.WHITE);
		mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
		mWebView.setWebViewClient(new WebViewClient() {
			public void onReceivedSslError(WebView view,
					SslErrorHandler handler, SslError error) {
				// handler.cancel(); // Android默认的处理方式
				handler.proceed(); // 接受所有网站的证书
				// handleMessage(Message msg); // 进行其他处理
			}

			@Override
			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				// TODO Auto-generated method stub
				super.onReceivedError(view, errorCode, description, failingUrl);
//				Log.d("test", "failed to load url:" + failingUrl);
			}

		});
		mWebView.addJavascriptInterface(new WebScript(), "dc");
		mWebView.getSettings().setJavaScriptEnabled(true);
		mWebView.getSettings().setLoadWithOverviewMode(true);
		mWebView.getSettings().setUseWideViewPort(true);
		mWebView.setWebChromeClient(new WebChromeClient() {
			@Override
			public boolean onJsAlert(WebView view, String url, String message,
					JsResult result) {
				// TODO Auto-generated method stub
				return false;
			}
		});
//		Log.d("test", "webfragment myView :" + this);
		return mWebView;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public void setTitle(String s) {
		mTitle = s;
	}

	@Override
	public boolean handleBackButton() {
		// TODO Auto-generated method stub
		if (mWebView.canGoBack()) {
			mWebView.goBack();
			return true;
		}
		return super.handleBackButton();
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onViewCreated(view, savedInstanceState);

		if (url != null)
			mWebView.loadUrl(url);
		else if (data != null) {
			mWebView.loadData(data, "text/html", "utf-8");
		} else {
			url = getActivity().getIntent().getStringExtra("url");
			if (url != null)
				mWebView.loadUrl(url);
		}
		pressBackToFinish = getActivity().getIntent().getBooleanExtra(
				"pressbacktofinish", false);
	}

	@Override
	public void onDetach() {
		// TODO Auto-generated method stub
		super.onDetach();
		this.dismissProgressDialog();
	}

	@Override
	public String titleText() {
		// TODO Auto-generated method stub
		return mTitle;
	}

	@Override
	protected boolean showDefaultSecondLeftButton() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (mSecondLeftItem != null && mSecondLeftItem.view == v) {
			if (pressBackToFinish) {
				getActivity().finish();
				return;
			}
		}
		super.onClick(v);
	}

	private class WebScript {
		@JavascriptInterface
		public void goGoodsList() {
			ViewUtils.runInMainThread(new Runnable() {

				@Override
				public void run() {
					if (WebViewFragment.this.getContainerActivity()
							.getMainActivity() != null) {
						WebViewFragment.this.getContainerActivity().popToRoot();
						WebViewFragment.this.getContainerActivity()
								.getMainActivity()
								.setCurrentTab(2, ProductListFragment.class);
					} else {
						WebViewFragment.this.getContainerActivity().finish();
						MainActivity.sharedInstance.setCurrentTab(2,
								ProductListFragment.class);
					}
				}
			});

		}

		@JavascriptInterface
		public void goOrderDetail() {
			// ViewUtils.runInMainThread(new Runnable() {
			//
			// @Override
			// public void run() {
			if (WebViewFragment.this.getContainerActivity().getMainActivity() != null) {
				WebViewFragment.this.getContainerActivity().pop(1);
			} else {
				WebViewFragment.this.getContainerActivity().finish();
			}
			// }
			// });

		}
	}
}
